﻿/**
* File: XmglxmxxService.cs
* Author: 汪杰
* Create Time:2025/2/21 13:53:00
* Email:15200358008@139.com
* 代码由TMCodeGenerator自动生成
**/
using AutoMapper;
using JSLCloud.App.Mes.Models;
using JSLCloud.App.Models.Report;
using JSLCloud.App.Models.Xmglxmxx;
using JSLCloud.App.Models.Xmglxmxxkz;
using JSLCloud.App.Models.Xmglxmxxsk;
using JSLCloud.App.Models.Xmjckhxx;
using JSLCloud.App.Models.Xmkhgltx;
using JSLCloud.Attributes;
using JSLCloud.DBModel;
using JSLCloud.Framework;
using JSLCloud.Models;
using JSLCloud.Utilities.SqlSugar;
using JSLCloud.Utilities.WebApi;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Threading.Tasks;


namespace JSLCloud.App.Mes.Service
{
    /// <summary>
    /// IXmglxmxxService 实现类
    /// </summary>
    [UseDI(ServiceLifetime.Scoped, typeof(IReportService))]
    public class ReportService : IReportService
    {
        private readonly IDbContext _db;//数据库操作实例对象
        private readonly ILogger<ReportService> _log;//日志操作实例对象
        private readonly IMapper _mapper;//AutoMapper实例

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="logger"></param>
        /// <param name="mapper"></param>
        public ReportService(IDbContext dbContext, ILogger<ReportService> logger, IMapper mapper)
        {
            _db = dbContext;
            _log = logger;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取xm_gl_xmxx数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseObject<List<XmglxmxxQueryModel>>> GetOrderReportAsync(RequestGet requestObject)
        {
            try
            {
                List<XmglxmxxQueryModel> queryData = null;//查询结果集对象
                RefAsync<int> totalNumber = -1;//总记录数
                var query = _db.Instance.Queryable<XmglxmxxDbModel, XmjcgltxDbModel, XmjckhxxDbModel>(
                    (t, t1, t2) => new object[]
                    {
                        JoinType.Left , t.Txid == t1.ID,
                        JoinType.Left , t.Khid == t2.ID
                    });
                //查询条件
                if (requestObject.QueryConditions != null && requestObject.QueryConditions.Count > 0)
                {
                    var conditionals = SqlSugarUtil.GetConditionalModels(requestObject.QueryConditions);
                    query.Where(conditionals);
                }

                //查询未删除数据
                query.Where("t.DELETEFLAG = 0");

                //执行查询
                if (requestObject.IsPaging)
                {
                    queryData = await query.Select(
                        (t, t1, t2) => new XmglxmxxQueryModel
                        {
                            ID = t.ID,
                            Createid = t.Createid,
                            Createname = t.Createname,
                            Createtime = t.Createtime,
                            Updateid = t.Updateid,
                            Updatename = t.Updatename,
                            Updatetime = t.Updatetime,
                            Deleteflag = t.Deleteflag,
                            Khid = t.Khid,
                            Txid = t.Txid,
                            Erpbh = t.Erpbh,
                            Sxq = t.Sxq,
                            Shlx = t.Shlx,
                            Txrs = t.Txrs,
                            Rzfw = t.Rzfw,
                            Csje = t.Csje,
                            Jsje = t.Jsje,
                            Htqdrq = t.Htqdrq,
                            Bz = t.Bz,
                            Khmc = t2.Khmc,
                            Txgg = t1.Txgg,
                            Txmc = t1.Txmc,
                            Rzbz = t1.Rzbz,
                            Shr = t.Shr,
                            Shsj = t.Shsj,
                            Shzt = t.Shzt,
                            Pcshr = t.Pcshr,
                            Pcshsj = t.Pcshsj,
                            Pcshzt = t.Pcshzt,
                            Pczt = t.Pczt,
                            Xmzt = t.Xmzt,
                            Skzt = t.Skzt,
                            Skqrr = t.Skqrr,
                            Skqrsj = t.Skqrsj,
                            Kpqrr = t.Kpqrr,
                            Kpqrsj = t.Kpqrsj,
                            Kpzt = t.Kpzt,
                            Xmztqrr = t.Xmztqrr,
                            Xmztqrsj = t.Xmztqrsj,
                            Htje = t.Htje

                        })
                        .ToPageListAsync(requestObject.PageIndex, requestObject.PageSize, totalNumber);
                }
                else
                {
                    queryData = await query.Select(
                        (t, t1, t2) => new XmglxmxxQueryModel
                        {
                            ID = t.ID,
                            Createid = t.Createid,
                            Createname = t.Createname,
                            Createtime = t.Createtime,
                            Updateid = t.Updateid,
                            Updatename = t.Updatename,
                            Updatetime = t.Updatetime,
                            Deleteflag = t.Deleteflag,
                            Khid = t.Khid,
                            Txid = t.Txid,
                            Erpbh = t.Erpbh,
                            Sxq = t.Sxq,
                            Shlx = t.Shlx,
                            Txrs = t.Txrs,
                            Rzfw = t.Rzfw,
                            Csje = t.Csje,
                            Jsje = t.Jsje,
                            Htqdrq = t.Htqdrq,
                            Bz = t.Bz,
                            Khmc = t2.Khmc,
                            Txgg = t1.Txgg,
                            Txmc = t1.Txmc,
                            Rzbz = t1.Rzbz,
                            Shr = t.Shr,
                            Shsj = t.Shsj,
                            Shzt = t.Shzt,
                            Pcshr = t.Pcshr,
                            Pcshsj = t.Pcshsj,
                            Pcshzt = t.Pcshzt,
                            Pczt = t.Pczt,
                            Xmzt = t.Xmzt,
                            Skzt = t.Skzt,
                            Skqrr = t.Skqrr,
                            Skqrsj = t.Skqrsj,
                            Kpqrr = t.Kpqrr,
                            Kpqrsj = t.Kpqrsj,
                            Kpzt = t.Kpzt,
                            Xmztqrr = t.Xmztqrr,
                            Xmztqrsj = t.Xmztqrsj,
                            Htje = t.Htje
                        })
                        .ToListAsync();
                }

                queryData.ForEach(async p =>
                {
                    p.Yskje = await _db.Instance.Queryable<XmglxmxxskDbModel>().Where(p1 => p1.Xmid == p.ID).SumAsync(p1 => p1.Skje);
                    p.Sjskje = await _db.Instance.Queryable<XmglxmxxskDbModel>().Where(p1 => p1.Xmid == p.ID && p1.Skzt == "Y").SumAsync(p1 => p1.Ssje) ?? 0;
                });

                //返回执行结果
                return ResponseUtil<List<XmglxmxxQueryModel>>.SuccessResult(queryData, totalNumber);
            }
            catch (Exception ex)
            {
                //返回查询异常结果
                return ResponseUtil<List<XmglxmxxQueryModel>>.FailResult(null, ex.Message);
            }
        }

        /// <summary>
        /// 获取xm_gl_xmxx数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseObject<List<XmjckhxxQueryReportModel>>> GetCustomReportAsync(RequestGet requestObject)
        {
            try
            {
                List<XmjckhxxQueryReportModel> queryData = null;//查询结果集对象
                RefAsync<int> totalNumber = -1;//总记录数
                var query = _db.Instance.Queryable<XmjckhxxDbModel>();
                //查询条件
                if (requestObject.QueryConditions != null && requestObject.QueryConditions.Count > 0)
                {
                    var conditionals = SqlSugarUtil.GetConditionalModels(requestObject.QueryConditions);
                    query.Where(conditionals);
                }
                //查询未删除数据
                query.Where("DELETEFLAG = 0");

                //执行查询
                if (requestObject.IsPaging)
                {
                    queryData = await query.Select(
                        (t) => new XmjckhxxQueryReportModel
                        {
                            ID = t.ID,
                            Khmc = t.Khmc,

                        })
                        .ToPageListAsync(requestObject.PageIndex, requestObject.PageSize, totalNumber);
                }
                else
                {
                    queryData = await query.Select(
                        (t) => new XmjckhxxQueryReportModel
                        {
                            ID = t.ID,
                            Khmc = t.Khmc,
                        })
                        .ToListAsync();
                }

                queryData.ForEach(async p =>
                {
                    var xmid = await _db.Instance.Queryable<XmglxmxxDbModel>().Where(p1 => p1.Khid == p.ID).Select(p1 => p1.ID).ToListAsync();
                    p.Htje = await _db.Instance.Queryable<XmglxmxxDbModel>().Where(p1 => p1.Khid == p.ID).SumAsync(p1 => p1.Htje);
                    p.Yskje = await _db.Instance.Queryable<XmglxmxxskDbModel>().Where(p1 => xmid.Contains(p1.Xmid)).SumAsync(p1 => p1.Skje);
                    p.Sjskje = await _db.Instance.Queryable<XmglxmxxskDbModel>().Where(p1 => xmid.Contains(p1.Xmid) && p1.Skzt == "Y").SumAsync(p1 => p1.Ssje) ?? 0;

                    var gltx = await _db.Instance.Queryable<XmkhgltxDbModel, XmjcgltxDbModel>
                                ((t, t1) => new object[] { JoinType.Left, t.Khid == t1.ID })
                                .Where((t, t1) => t.Khid == p.ID)
                                .Select((t, t1) => new
                                {
                                    t.Khid,
                                    t1.Txmc,
                                    t1.Txgg,
                                    t1.Rztxzq,
                                    t1.Jstxzq,
                                    t1.Jszq,
                                    t.Zsyxq
                                })
                                .ToListAsync();
                    p.Ygq = gltx.Count(p1 => p1.Zsyxq < DateTime.Now);
                    p.Jgq = gltx.Count(p1 => p1.Zsyxq.AddDays(p1.Rztxzq * -1) < DateTime.Now && p1.Zsyxq > DateTime.Now);
                    p.Djd1 = gltx.Count(p1 => p1.Zsyxq.AddYears(p1.Jszq).AddDays(p1.Jstxzq * -1) < DateTime.Now
                    && p1.Zsyxq.AddYears(p1.Jszq) < p1.Zsyxq.AddYears(p1.Jszq + p1.Jszq));
                    p.Djd2 = gltx.Count(p1 => p1.Zsyxq.AddYears(p1.Jszq + p1.Jszq).AddDays(p1.Jstxzq * -1) < DateTime.Now);

                });

                //返回执行结果
                return ResponseUtil<List<XmjckhxxQueryReportModel>>.SuccessResult(queryData, totalNumber);
            }
            catch (Exception ex)
            {
                //返回查询异常结果
                return ResponseUtil<List<XmjckhxxQueryReportModel>>.FailResult(null, ex.Message);
            }
        }

        /// <summary>
        /// 获取xm_gl_xmxx数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseObject<List<XmkhgltxReportModel>>> GetCertificateReportAsync(RequestGet requestObject)
        {
            try
            {
                RefAsync<int> totalNumber = -1;//总记录数
                var queryData = await _db.Instance.Queryable<XmkhgltxDbModel, XmjckhxxDbModel, XmjcgltxDbModel>(
                    (t, t1, t2) => new object[]
                    {
                        JoinType.Left , t.Khid == t1.ID,
                        JoinType.Left , t.Txid == t2.ID,
                    })
                    .Where((t,t1,t2) => t.Zslb == "主证书" && t.Zszt == "有效")
                    .Select((t, t1, t2) => new XmkhgltxReportModel
                    {
                        Khid = t.Khid,
                        Khmc = t1.Khmc,
                        Txid = t.Txid,
                        Txmc = t2.Txmc,
                        Mcshrq = (DateTime)t.Mcshrq,
                        Xcshrq = (DateTime)t.Xcshrq,
                        Zsrq = t.Zsrq,
                        Zsyxq = t.Zsyxq,
                        Zsh = t.Zsh,
                        Dshlx = t.Dshlx
                    })
                    .ToListAsync();
                string khmc = "";
                string txmc = "";
                var start = DateTime.Now.AddYears(-50);
                var end = DateTime.Now.AddYears(50);
                foreach (var item in requestObject.QueryConditions)
                {
                    if (item.Column == "khmc")
                    {
                        khmc = item.Content;
                    }
                    if (item.Column == "txmc")
                    {
                        txmc = item.Content;
                    }
                    if (item.Column == "auditing")
                    {
                        start = Convert.ToDateTime(item.Content.Split(',')[0]);
                        end = Convert.ToDateTime(item.Content.Split(',')[1]);
                    }
                }
                var resultData = new List<XmkhgltxReportModel>();

                foreach (var item in queryData)
                {
                    if (!string.IsNullOrEmpty(khmc) && !item.Khmc.Contains(khmc))
                    {
                        //非查询客户
                        continue;
                    }
                    if (!string.IsNullOrEmpty(txmc) && !item.Txmc.Contains(txmc))
                    {
                        //非查询体系
                        continue;
                    }
                    if(item.Dshlx == "监一")
                    {
                        var j1start = item.Mcshrq.AddMonths(9);
                        var j1end = item.Mcshrq.AddMonths(15);
                        if((j1start >= start && j1start <= end) || (j1end >= start && j1end <= end))
                        {
                            resultData.Add(item);
                        }
                    }
                    if(item.Dshlx == "监二")
                    {
                        var j2start = item.Mcshrq.AddMonths(21);
                        var j2end = item.Mcshrq.AddMonths(27);
                        if ((j2start >= start && j2start <= end) || (j2end >= start && j2end <= end))
                        {
                            resultData.Add(item);
                        }
                    }
                }


                //返回执行结果
                return ResponseUtil<List<XmkhgltxReportModel>>.SuccessResult(resultData, totalNumber);
            }
            catch (Exception ex)
            {
                //返回查询异常结果
                return ResponseUtil<List<XmkhgltxReportModel>>.FailResult(null, ex.Message);
            }
        }

        /// <summary>
        /// 获取xm_gl_xmxx数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseObject<List<float>>> GetOrderDataAsync()
        {
            try
            {
                List<float> queryData = new List<float>();
                for (var i = 1; i <= 12; i++)
                {
                    var start = Convert.ToDateTime($"{DateTime.Now.Year}-{i}-01");
                    var end = Convert.ToDateTime($"{start.AddMonths(1):yyyy-MM}-01");
                    var curr = 0m;
                    var currDataList = await _db.Instance.Queryable<XmglxmxxDbModel>()
                        .Where(p => p.Deleteflag == 0 && p.Shzt == "Y" && p.Htqdrq >= start && p.Htqdrq < end)
                        .ToListAsync();
                    foreach(var item in currDataList)
                    {
                        if(item.Shlx == "监一" || item.Shlx == "监二")
                        {
                            curr += item.Jsje;
                        }
                        else
                        {
                            curr += item.Csje;
                        }
                    }
                    queryData.Add((float)curr);
                }


                //返回执行结果
                return ResponseUtil<List<float>>.SuccessResult(queryData);
            }
            catch (Exception ex)
            {
                //返回查询异常结果
                return ResponseUtil<List<float>>.FailResult(null, ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseObject<FirstPageQueryModel>> GetFirstDataAsync()
        {
            try
            {
                var result = new FirstPageQueryModel();
                var start = Convert.ToDateTime($"{DateTime.Now.Year}-01-01");
                var end = Convert.ToDateTime($"{DateTime.Now.Year}-12-31");
                
                //订单总计，当年
                result.Ddje = await _db.Instance.Queryable<XmglxmxxDbModel>()
                    .Where(p => p.Deleteflag == 0 && p.Shzt == "Y" && p.Zslb == "主证书" && p.Htqdrq >= start && p.Htqdrq <= end)
                    .SumAsync(p => p.Htje);
                //待收款，所有
                result.Dskje = await _db.Instance.Queryable<XmglxmxxskDbModel>()
                    .Where(p => p.Deleteflag == 0 && p.Skzt == "N")
                    .SumAsync(p => p.Skje);

                result.Djytx = new List<XmkhgltxReportModel>();
                result.Djetx = new List<XmkhgltxReportModel>();
                result.Drztx = new List<XmkhgltxReportModel>();

                #region 待认证 监一  监二
                var queryData = await _db.Instance.Queryable<XmkhgltxDbModel, XmjckhxxDbModel, XmjcgltxDbModel>(
                    (t, t1, t2) => new object[]
                    {
                        JoinType.Left , t.Khid == t1.ID,
                        JoinType.Left , t.Txid == t2.ID,
                    })
                    .Where((t, t1, t2) => t.Zslb == "主证书")
                    .Select((t, t1, t2) => new XmkhgltxReportModel
                    {
                        Khid = t.Khid,
                        Khmc = t1.Khmc,
                        Txid = t.Txid,
                        Txmc = t2.Txmc,
                        Mcshrq = (DateTime)t.Mcshrq,
                        Xcshrq = (DateTime)t.Xcshrq,
                        Zsrq = t.Zsrq,
                        Zsyxq = t.Zsyxq,
                        Zsh = t.Zsh
                    })
                    .ToListAsync();
                

                foreach (var item in queryData)
                {
                    //查询是否有监一项目信息
                    var j1Model = await _db.Instance.Queryable<XmglxmxxDbModel>()
                        .Where(p => p.Khid == item.Khid && p.Txid == item.Txid && p.Shxh == 1 && p.Zsh == item.Zsh)
                        .FirstAsync();
                    if (j1Model == null)
                    {
                        //监一
                        var j1s = item.Zsrq.AddYears(1);
                        var j1e = item.Zsrq.AddYears(1).AddMonths(6);
                        var j1z = item.Zsrq.AddYears(1).AddMonths(6).AddDays(1);
                        if ((j1s >= start && j1s <= end) || (j1e >= start && j1e <= end))
                        {
                            //检查是否已生成该体系的再认证信息
                            var zrzxmxx = await _db.Instance.Queryable<XmglxmxxDbModel>()
                                .Where(p => p.Khid == item.Khid && p.Txid >= item.Txid && p.Zsh == item.Zsh && p.Shxh == 1)
                                .FirstAsync();
                            if (zrzxmxx == null)
                            {
                                item.Dshlx = "监一";
                                result.Djy++;
                                result.Djytx.Add(item);
                                continue;
                            }
                        }
                        if (DateTime.Now >= j1z)
                        {
                            //检查是否已生成该体系的再认证信息
                            var zrzxmxx = await _db.Instance.Queryable<XmglxmxxDbModel>()
                                .Where(p => p.Khid == item.Khid && p.Txid >= item.Txid && p.Createtime > item.Zsrq && p.Shxh == 0)
                                .FirstAsync();
                            if (zrzxmxx == null)
                            {
                                item.Dshlx = "再认证";
                                result.Drz++;
                                result.Drztx.Add(item);
                                continue;
                            }
                        }
                        continue;
                    }
                    //监二
                    var j2Model = await _db.Instance.Queryable<XmglxmxxDbModel>()
                       .Where(p => p.Khid == item.Khid && p.Txid == item.Txid && p.Shxh == 2 && p.Zsh == item.Zsh)
                       .FirstAsync();
                    if (j2Model == null)
                    {
                        var j2s = item.Zsrq.AddYears(2);
                        var j2e = item.Zsrq.AddYears(2).AddMonths(6);
                        var j2z = item.Zsrq.AddYears(2).AddMonths(6).AddDays(1);
                        if ((j2s >= start && j2s <= end) || (j2e >= start && j2e <= end))
                        {
                            var zrzxmxx = await _db.Instance.Queryable<XmglxmxxDbModel>()
                                .Where(p => p.Khid == item.Khid && p.Txid >= item.Txid && p.Zsh == item.Zsh && p.Shxh == 2)
                                .FirstAsync();
                            if (zrzxmxx == null)
                            {
                                item.Dshlx = "监二";
                                result.Dje++;
                                result.Djetx.Add(item);
                                continue;
                            }
                        }
                        if (DateTime.Now >= j2z)
                        {
                            //检查是否已生成该体系的再认证信息
                            var zrzxmxx = await _db.Instance.Queryable<XmglxmxxDbModel>()
                                .Where(p => p.Khid == item.Khid && p.Txid >= item.Txid && p.Createtime > item.Zsrq && p.Shxh == 0)
                                .FirstAsync();
                            if (zrzxmxx == null)
                            {
                                item.Dshlx = "再认证";
                                result.Dje++;
                                result.Djetx.Add(item);
                                continue;
                            }
                        }
                        continue;
                    }

                    if (item.Zsyxq >= start || item.Zsyxq <= end)
                    {
                        //检查是否已生成该体系的再认证信息
                        var zrzxmxx = await _db.Instance.Queryable<XmglxmxxDbModel>()
                            .Where(p => p.Khid == item.Khid && p.Txid >= item.Txid && p.Createtime > item.Zsrq && p.Shxh == 0)
                            .FirstAsync();
                        if (zrzxmxx == null)
                        {
                            item.Dshlx = "再认证";
                            result.Drztx.Add(item);
                            result.Drz++;
                        }
                    }
                }
                #endregion




                //返回执行结果
                return ResponseUtil<FirstPageQueryModel>.SuccessResult(result);
            }
            catch (Exception ex)
            {
                //返回查询异常结果
                return ResponseUtil<FirstPageQueryModel>.FailResult(null, ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseObject<FirstPageQueryModel>> GetFirstData2Async()
        {
            try
            {
                var result = new FirstPageQueryModel();

                var queryData = await _db.Instance.Queryable<XmkhgltxDbModel, XmjckhxxDbModel, XmjcgltxDbModel>(
                    (t, t1, t2) => new object[]
                    {
                        JoinType.Left , t.Khid == t1.ID,
                        JoinType.Left , t.Txid == t2.ID,
                    })
                    .Where((t, t1, t2) => t.Zslb == "主证书")
                    .Select((t, t1, t2) => new XmkhgltxReportModel
                    {
                        Khid = t.Khid,
                        Khmc = t1.Khmc,
                        Txid = t.Txid,
                        Txmc = t2.Txmc,
                        Mcshrq = (DateTime)t.Mcshrq,
                        Xcshrq = (DateTime)t.Xcshrq,
                        Zsrq = t.Zsrq,
                        Zsyxq = t.Zsyxq,
                        Zsh = t.Zsh,
                        Dshlx = t.Dshlx,
                    })
                    .ToListAsync();

                result.Djytx = queryData.Where(p => p.Dshlx == "监一").ToList();
                result.Djetx = queryData.Where(p => p.Dshlx == "监二").ToList();

                //待收款
                var end = DateTime.Now.AddMonths(1).Date;
                var dskData = await _db.Instance.Queryable<XmglxmxxskDbModel, XmglxmxxDbModel, XmjcgltxDbModel, XmjckhxxDbModel>(
                    (t, t1, t2, t3) => new object[]
                    {
                        JoinType.Left , t.Xmid == t1.ID,
                        JoinType.Left , t1.Txid == t2.ID,
                        JoinType.Left , t1.Khid == t3.ID,
                    })
                    .Where((t, t1, t2, t3) => t.Ysrq <= end && t.Skzt == "N" && t.Deleteflag == 0)
                    .Select((t, t1, t2, t3) => new XmglxmxxskQueryModel
                    {
                        Khmc = t3.Khmc,
                        Txmc = t2.Txmc,
                        Ysrq = t.Ysrq,
                        Skxm = t.Skxm
                    })
                    .ToListAsync();

                result.Dsk = dskData;

                //待付款
                var dkzData = await _db.Instance.Queryable<XmglxmxxkzDbModel, XmglxmxxDbModel, XmjcgltxDbModel, XmjckhxxDbModel>(
                    (t, t1, t2, t3) => new object[]
                    {
                        JoinType.Left , t.Xmid == t1.ID,
                        JoinType.Left , t1.Txid == t2.ID,
                        JoinType.Left , t1.Khid == t3.ID,
                    })
                    .Where((t, t1, t2, t3) => t.Kzsj <= end && t.Kzzt == "N" && t.Deleteflag == 0)
                    .Select((t, t1, t2, t3) => new XmglxmxxkzQueryModel
                    {
                        Khmc = t3.Khmc,
                        Txmc = t2.Txmc,
                        Skf = t.Skf,
                        Kzxm = t.Kzxm
                    })
                    .ToListAsync();

                result.Dkz = dkzData;

                //返回执行结果
                return ResponseUtil<FirstPageQueryModel>.SuccessResult(result);
            }
            catch (Exception ex)
            {
                //返回查询异常结果
                return ResponseUtil<FirstPageQueryModel>.FailResult(null, ex.Message);
            }
        }


        /// <summary>
        /// 获取xm_gl_xmxx数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseObject<List<float>>> GetReceiveDataAsync()
        {
            try
            {
                List<float> queryData = new List<float>();
                for (var i = 1; i <= 12; i++)
                {
                    var start = Convert.ToDateTime($"{DateTime.Now.Year}-{i}-01");
                    var end = start.AddMonths(1);
                    var curr = await _db.Instance.Queryable<XmglxmxxskDbModel>()
                        .Where(p => p.Deleteflag == 0 && p.Skzt == "Y" && p.Skrq >= start && p.Skrq < end)
                        .SumAsync(p => p.Ssje) ?? 0;
                    queryData.Add((float)curr);
                    i++;
                }


                //返回执行结果
                return ResponseUtil<List<float>>.SuccessResult(queryData);
            }
            catch (Exception ex)
            {
                //返回查询异常结果
                return ResponseUtil<List<float>>.FailResult(null, ex.Message);
            }
        }

        /// <summary>
        /// 获取xm_gl_xmxx数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseObject<List<float>>> GetExpensesDataAsync()
        {
            try
            {
                List<float> queryData = new List<float>();
                for (var i = 1; i <= 12; i++)
                {
                    var start = Convert.ToDateTime($"{DateTime.Now.Year}-{i}-01");
                    var end = start.AddMonths(1);
                    var curr = await _db.Instance.Queryable<XmglxmxxkzDbModel>()
                        .Where(p => p.Deleteflag == 0 && p.Kzzt == "Y" && p.Kzsj >= start && p.Kzsj < end)
                        .SumAsync(p => p.Kzje);
                    queryData.Add((float)curr);
                    i++;
                }


                //返回执行结果
                return ResponseUtil<List<float>>.SuccessResult(queryData);
            }
            catch (Exception ex)
            {
                //返回查询异常结果
                return ResponseUtil<List<float>>.FailResult(null, ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="requestObject"></param>
        /// <returns></returns>
        public async Task<ResponseObject<List<WorktimeQueryModel>>> GetWorktimeReportAsync(RequestGet requestObject)
        {
            try
            {
                RefAsync<int> totalNumber = -1;//总记录数
                if (requestObject.QueryConditions.Count < 1)
                {
                    return ResponseUtil<List<WorktimeQueryModel>>.FailResult(null, "请输入查询月份");
                }

                var query = _db.Instance.Queryable<XmglxmxxpcDbModel>();
                DateTime start = new DateTime();
                DateTime end = new DateTime();
                foreach (var item in requestObject.QueryConditions)
                {
                    if(item.Column == "auditmonth")
                    {
                        start = Convert.ToDateTime($"{item.Content}-01 00:00:00");
                        end = start.AddMonths(1);
                        query.Where(p => p.Kssj >= start || p.Jssj < end);
                    }
                    else
                    {
                        var conditionals = SqlSugarUtil.GetConditionalModels(requestObject.QueryConditions);
                        query.Where(conditionals);
                    }
                }

                var data = await query.ToListAsync();
                var dic = new Dictionary<int, WorktimeQueryModel>();
                foreach(var item in data)
                {
                    if (!dic.ContainsKey(item.Shyid))
                    {
                        dic.Add(item.Shyid, new WorktimeQueryModel
                        {
                            Id = item.Shyid,
                            Xm = item.Shy
                        });
                    }
                    var count = 0f;
                    var currRt = 0f;
                    for (var sday = item.Kssj; sday <= item.Jssj; sday = sday.AddDays(1))
                    {
                        if (sday < start || sday >= end)
                        {
                            continue;
                        }
                        if (item.Rt - count - 1 > 0)
                        {
                            currRt = 1;
                            count++;
                        }
                        else
                        {
                            currRt = item.Rt - count;
                        }

                        switch (sday.Day)
                        {
                            case 1:
                                dic[item.Shyid].Day1 = currRt;
                                break;
                            case 2:
                                dic[item.Shyid].Day2 = currRt;
                                break;
                            case 3:
                                dic[item.Shyid].Day3 = currRt;
                                break;
                            case 4:
                                dic[item.Shyid].Day4 = currRt;
                                break;
                            case 5:
                                dic[item.Shyid].Day5 = currRt;
                                break;
                            case 6:
                                dic[item.Shyid].Day6 = currRt;
                                break;
                            case 7:
                                dic[item.Shyid].Day7 = currRt;
                                break;
                            case 8:
                                dic[item.Shyid].Day8 = currRt;
                                break;
                            case 9:
                                dic[item.Shyid].Day9 = currRt;
                                break;
                            case 10:
                                dic[item.Shyid].Day10 = currRt;
                                break;
                            case 11:
                                dic[item.Shyid].Day11 = currRt;
                                break;
                            case 12:
                                dic[item.Shyid].Day12 = currRt;
                                break;
                            case 13:
                                dic[item.Shyid].Day13 = currRt;
                                break;
                            case 14:
                                dic[item.Shyid].Day14 = currRt;
                                break;
                            case 15:
                                dic[item.Shyid].Day15 = currRt;
                                break;
                            case 16:
                                dic[item.Shyid].Day16 = currRt;
                                break;
                            case 17:
                                dic[item.Shyid].Day17 = currRt;
                                break;
                            case 18:
                                dic[item.Shyid].Day18 = currRt;
                                break;
                            case 19:
                                dic[item.Shyid].Day19 = currRt;
                                break;
                            case 20:
                                dic[item.Shyid].Day20 = currRt;
                                break;
                            case 21:
                                dic[item.Shyid].Day21 = currRt;
                                break;
                            case 22:
                                dic[item.Shyid].Day22 = currRt;
                                break;
                            case 23:
                                dic[item.Shyid].Day23 = currRt;
                                break;
                            case 24:
                                dic[item.Shyid].Day24 = currRt;
                                break;
                            case 25:
                                dic[item.Shyid].Day25 = currRt;
                                break;
                            case 26:
                                dic[item.Shyid].Day26 = currRt;
                                break;
                            case 27:
                                dic[item.Shyid].Day27 = currRt;
                                break;
                            case 28:
                                dic[item.Shyid].Day28 = currRt;
                                break;
                            case 29:
                                dic[item.Shyid].Day29 = currRt;
                                break;
                            case 30:
                                dic[item.Shyid].Day30 = currRt;
                                break;
                            case 31:
                                dic[item.Shyid].Day31 = currRt;
                                break;


                        }

                        dic[item.Shyid].Zrt += currRt;
                    }
                }

                //返回执行结果
                return ResponseUtil<List<WorktimeQueryModel>>.SuccessResult(dic.Values.ToList());
            }
            catch (Exception ex)
            {
                //返回查询异常结果
                return ResponseUtil<List<WorktimeQueryModel>>.FailResult(null, ex.Message);
            }
        }

    }
}
